<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreBreadcrumb {
	var $_trail;
	
	//constructor
	function CoreBreadcrumb() {
		$this->reset();
	}
	
	//methods
	function init() {
		$this->reset();
		
		$this->addHome();
	}
	
	function reset() {
		$this->_trail = array();
	}
	
	function add($title, $link = '', $domain = '') {
		$this->_trail[] = array('title' => $title, 'link' => $link, 'domain' => $domain);
	}
	
	function addHome() {
		$this->_trail[] = array('title' => ':Home', 'link' => 'home', 'domain' => '');
	}
	
	function addLink($link) {
		$this->_trail[] = array('title' => ':TITLE_' . strtoupper($link), 'link' => $link . '/', 'domain' => '');
	}
	
	function trail($separator = ' - ') {
		$trail_string = '';
		
		for($i=0, $n=sizeof($this->_trail); $i<$n; $i++) {
			if( isset($this->_trail[$i]['link']) && util_not_null($this->_trail[$i]['link']) ) {
				$trail_string .= '<a href="' . $this->_trail[$i]['link'] . '">' . $this->_trail[$i]['title'] . '</a>';
			} else {
				$trail_string .= $this->_trail[$i]['title'];
			}
		
			if( ($i+1) < $n) $trail_string .= $separator;
		}
		
		return $trail_string;
	}
	
	function toString($style = 'ul') {
		global $LANGUAGE;
		
		$string = '';
		if( is_array($this->_trail) ) {
			if( $style == 'ul' ) {
				$string .= '<ul>';
			}
			for($i=0, $n=sizeof($this->_trail); $i<$n; $i++) {
				$title = $this->_trail[$i]['title'];
				$link = $this->_trail[$i]['link'];
				$domain = $this->_trail[$i]['domain'];
				
				//
				if( substr($title, 0, strlen(':')) == ':' ) {
					$ks = explode(':', $title);
					if( sizeof($ks) == 2 ) {
						if( !isset($LANGUAGE[$ks['1']]) || is_array($LANGUAGE[$ks['1']]) ) {
							$title = ucfirst(str_replace('_', ' ', $ks['1']));
						} else {
							$title = $LANGUAGE[$ks['1']];
						}
					} else {
						$title = isset($LANGUAGE[$ks['1']][$ks['2']]) ? $LANGUAGE[$ks['1']][$ks['2']] : $ks['2'];
					}
				}
				
				//
				if( $link != '' ) {
					if( $link == 'home' ) {
						$link = CONFIG_URL_HTTP;
					} else {
						if( $domain == '' ) {
							$link = CONFIG_URL_HTTP . $link;
						} else {
							$link = $domain . $link;
						}
					}
					$string .= '<li><a href="' . $link . '">';
					$string .= $title;
					$string .= '</a></li>';
				} else {
					$string .= '<li>' . $title . '</li>';
				}
				if( $i + 1 < $n ) {
					$string .= '<li>' . '&gt;&gt;' . '</li>';
				}
			}
			if( $style == 'ul' ) {
				$string .= '</ul>';
			}
		}
		
		return $string;
	}
}
//
?>